﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace Server
{
    public partial class AttendenceCheckForm : Form
    {
        public event ActiveAttendenceCheckEventHandler OnActiveAttendenceCheck = null;
        public event FormExitEventHandler OnFormExit = null;
        public event SaveListStudentsToFileEventHandler OnSaveListStudentsToFile = null;
        private Thread t = null;

        public AttendenceCheckForm()
        {
            InitializeComponent();
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            if (this.OnActiveAttendenceCheck != null)
            {
                this.OnActiveAttendenceCheck(this, new ActiveAttendenceCheckEventArgs());
            }
            this.btnStart.Enabled = false;
        }

        private void ShowSaveFileDiaglog()
        {
            if (this.saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                if (this.OnSaveListStudentsToFile != null)
                {
                    this.OnSaveListStudentsToFile(this, new SaveListStudentsToFileEventArgs(this.saveFileDialog1.FileName));
                }
            }
            if (this.t != null)
            {
                this.t.Abort();
            }
        }

        private void btnSaveFile_Click(object sender, EventArgs e)
        {
            this.t = new Thread(new ThreadStart(ShowSaveFileDiaglog));
            this.t.Start();            
        }

        private void AttendenceCheckForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (this.OnFormExit != null)
            {
                this.OnFormExit(this, new FormExitEvenArgs());
            }
            if (this.t != null)
            {
                this.t.Abort();
            }
        }
    }
}
